﻿Imports System.Data.OleDb
Public Class NhaCungCapDAO
    Public Function GetNhaCungCap() As DataTable
        Dim sql As String = "select * from NhaCungCap"
        Dim dt As New DataTable
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim da As New OleDbDataAdapter(sql, cn)
        da.Fill(dt)
        cn.Close()
        Return dt
    End Function

    Public Sub AddNhaCungCap(ByVal newCC As NhaCungCapDTO)
        Dim sql As String = String.Format("insert into NhaCungCap(MaNhaCungCap,TenNhaCungCap,TenNhomNL,SoDienThoai,DiaChi) values('{0}','{1}','{2}','{3}','{4}')", _
                                           newCC.MaCC, newCC.TenCC, newCC.NhomNguyenLieu, newCC.SDT, newCC.DiaChi)

        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Dữ liêu bị sai vui lòng kiểm tra lại !")
        End Try
        cn.Close()
    End Sub

    Public Sub DelNhaCungCap(ByVal delCC As NhaCungCapDTO)
        Dim sql As String = String.Format("delete from NhaCungCap where STT={0}", delCC.STT)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        cmd.ExecuteNonQuery()
        cn.Close()
    End Sub

    Public Sub UpdateNhaCungCap(ByVal newCC As NhaCungCapDTO)
        Dim sql As String = String.Format("update NhaCungCap set MaNhaCungCap='{0}',TenNhaCungCap='{1}',TenNhomNL='{2}',SoDienThoai='{3}',DiaChi='{4}' where STT={5}", _
                                          newCC.MaCC, newCC.TenCC, newCC.NhomNguyenLieu, newCC.SDT, newCC.DiaChi, newCC.STT)
        Dim cn As OleDbConnection = DataProvider.OpenConnect
        Dim cmd As New OleDbCommand(sql, cn)
        Try
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            MessageBox.Show("Vui lòng kiểu tra lại dữ liệu !")
        End Try
        cn.Close()
    End Sub
End Class
